package com.ruoyi.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.domain.WmsInWarehouseApplyDetail;
import com.ruoyi.mapper.WmsInWarehouseApplyMapper;
import com.ruoyi.domain.WmsInWarehouseApply;
import com.ruoyi.service.IWmsInWarehouseApplyService;

/**
 * 入库申请Service业务层处理
 * 
 * @author yinqing
 * @date 2025-06-13
 */
@Service
public class WmsInWarehouseApplyServiceImpl implements IWmsInWarehouseApplyService 
{
    @Autowired
    private WmsInWarehouseApplyMapper wmsInWarehouseApplyMapper;

    /**
     * 查询入库申请
     * 
     * @param id 入库申请主键
     * @return 入库申请
     */
    @Override
    public WmsInWarehouseApply selectWmsInWarehouseApplyById(Long id)
    {
        return wmsInWarehouseApplyMapper.selectWmsInWarehouseApplyById(id);
    }

    /**
     * 查询入库申请列表
     * 
     * @param wmsInWarehouseApply 入库申请
     * @return 入库申请
     */
    @Override
    public List<WmsInWarehouseApply> selectWmsInWarehouseApplyList(WmsInWarehouseApply wmsInWarehouseApply)
    {
        return wmsInWarehouseApplyMapper.selectWmsInWarehouseApplyList(wmsInWarehouseApply);
    }

    /**
     * 新增入库申请
     * 
     * @param wmsInWarehouseApply 入库申请
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsInWarehouseApply(WmsInWarehouseApply wmsInWarehouseApply)
    {
        int rows = wmsInWarehouseApplyMapper.insertWmsInWarehouseApply(wmsInWarehouseApply);
        insertWmsInWarehouseApplyDetail(wmsInWarehouseApply);
        return rows;
    }

    /**
     * 修改入库申请
     * 
     * @param wmsInWarehouseApply 入库申请
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsInWarehouseApply(WmsInWarehouseApply wmsInWarehouseApply)
    {
        wmsInWarehouseApplyMapper.deleteWmsInWarehouseApplyDetailByApplyId(wmsInWarehouseApply.getId());
        insertWmsInWarehouseApplyDetail(wmsInWarehouseApply);
        return wmsInWarehouseApplyMapper.updateWmsInWarehouseApply(wmsInWarehouseApply);
    }

    /**
     * 批量删除入库申请
     * 
     * @param ids 需要删除的入库申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsInWarehouseApplyByIds(Long[] ids)
    {
        wmsInWarehouseApplyMapper.deleteWmsInWarehouseApplyDetailByApplyIds(ids);
        return wmsInWarehouseApplyMapper.deleteWmsInWarehouseApplyByIds(ids);
    }

    /**
     * 删除入库申请信息
     * 
     * @param id 入库申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsInWarehouseApplyById(Long id)
    {
        wmsInWarehouseApplyMapper.deleteWmsInWarehouseApplyDetailByApplyId(id);
        return wmsInWarehouseApplyMapper.deleteWmsInWarehouseApplyById(id);
    }

    /**
     * 新增入库申请明细信息
     * 
     * @param wmsInWarehouseApply 入库申请对象
     */
    public void insertWmsInWarehouseApplyDetail(WmsInWarehouseApply wmsInWarehouseApply)
    {
        List<WmsInWarehouseApplyDetail> wmsInWarehouseApplyDetailList = wmsInWarehouseApply.getWmsInWarehouseApplyDetailList();
        Long id = wmsInWarehouseApply.getId();
        if (StringUtils.isNotNull(wmsInWarehouseApplyDetailList))
        {
            List<WmsInWarehouseApplyDetail> list = new ArrayList<WmsInWarehouseApplyDetail>();
            for (WmsInWarehouseApplyDetail wmsInWarehouseApplyDetail : wmsInWarehouseApplyDetailList)
            {
                wmsInWarehouseApplyDetail.setApplyId(id);
                list.add(wmsInWarehouseApplyDetail);
            }
            if (list.size() > 0)
            {
                wmsInWarehouseApplyMapper.batchWmsInWarehouseApplyDetail(list);
            }
        }
    }
}
